
package com.lzyc.zbar.decoding;
import net.sourceforge.zbar.Config;
import net.sourceforge.zbar.Image;
import net.sourceforge.zbar.ImageScanner;
import net.sourceforge.zbar.Symbol;
import net.sourceforge.zbar.SymbolSet;

import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;

import com.lzyc.ybtappcal.R;
import com.lzyc.zbar.activity.CaptureActivity;

final class DecodeHandler extends Handler 
{

  private static final String TAG = DecodeHandler.class.getSimpleName();
  private final CaptureActivity activity;
  private ImageScanner scanner;
  
  static
  {
    System.loadLibrary("iconv");
  }

  public DecodeHandler(CaptureActivity activity) 
  {
	 this.activity = activity;
	 scanner=new ImageScanner();
     scanner.setConfig(0,Config.X_DENSITY,3);
     scanner.setConfig(0, Config.Y_DENSITY, 3); 

  }

  @Override
  public void handleMessage(Message message) {
    switch (message.what) {
      case R.id.decode:
        decode((byte[]) message.obj, message.arg1, message.arg2);
        break;
      case R.id.quit:
        Looper.myLooper().quit();
        break;
    }
  }

  /**
   * Decode the data within the viewfinder rectangle, and time how long it took. For efficiency,
   * reuse the same reader objects from one decode to the next.
   *
   * @param data   The preview frame.
   * @param width  The width of the preview frame.
   * @param height The height of the preview frame.
   */
  private void decode(byte[] data, int width, int height) {

    Image barcode = new Image(width, height, "Y800");
	Rect scanImageRect = activity.getViewfinderView().getScanImageRect(height,width);
    barcode.setCrop(scanImageRect.top, scanImageRect.left, scanImageRect.bottom, scanImageRect.right);
    barcode.setData(data);

    int result = scanner.scanImage(barcode);
    String strResult="";
    if (result != 0) 
    {
       SymbolSet syms = scanner.getResults();
       for (Symbol sym : syms)
       {
    	   strResult=sym.getData().trim();
    	   if(!strResult.isEmpty())
    	   {
             break;
    	   }
       }
    }
    
    if(!strResult.isEmpty())
    {
       Message message = Message.obtain(activity.getHandler(), R.id.decode_succeeded, strResult);//Message信息传来传去,有点绕
       Log.d(TAG, "Sending decode succeeded message...");
        Bundle bundle=new Bundle();
        bundle.putByteArray("byte",data);
        bundle.putInt("w",width);
        bundle.putInt("h",height);
        message.setData(bundle);
       message.sendToTarget();
    } 
    else 
    {
        Message message = Message.obtain(activity.getHandler(), R.id.decode_failed);
        message.sendToTarget();
    }
  }

}
